package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.starseas_phone.Adapters.GuidePageAdapter;

import java.util.ArrayList;
import java.util.List;

public class GuidePageActivity extends AppCompatActivity {

    private List<View> list;
    private Button guidePage_EnterButton;
    private LinearLayout guidePage_point;
    private ViewPager guidePage_ViewPager;
    private String ip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_page);
        guidePage_EnterButton = (Button) findViewById(R.id.GuidePage_EnterButton);
        guidePage_point = (LinearLayout) findViewById(R.id.GuidePage_point);
        guidePage_ViewPager = (ViewPager) findViewById(R.id.GuidePage_ViewPager);
        SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
        ip = pref.getString("ip","");
        if (ip.equals("")||ip==null){
            // 设置点击监听
            clickDealWith();
            addView();
        }else {
            Log.d("有数据","yes");
            Intent intent = new Intent(GuidePageActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    //最后的按钮
    public void clickDealWith(){
        guidePage_EnterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                editor.putString("ip","已使用");
                editor.apply();
                Intent intent = new Intent(GuidePageActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
        guidePage_ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for(int i=0;i< 3;i++){
                    if(i == position){
                        guidePage_point.getChildAt(position).setBackgroundResource(R.drawable.xuanzhon);
                    }else{
                        guidePage_point.getChildAt(i).setBackgroundResource(R.drawable.weixuanzhon);
                    }
                    //判断是否滑动到最后一页，如果滑动到了最后一页则显示马上体验button
                    if (position == 2){
                        guidePage_EnterButton.setVisibility(View.VISIBLE);//显示button
                    }else {
                        guidePage_EnterButton.setVisibility(View.GONE);//不显示button
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //    添加图片
    private void addView(){
        list  = new ArrayList<>();
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewPager.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        ImageView iv = new ImageView(GuidePageActivity.this);
        ImageView iv2 = new ImageView(GuidePageActivity.this);
        ImageView iv3 = new ImageView(GuidePageActivity.this);
        iv.setLayoutParams(layoutParams);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setImageResource(R.drawable.ydy1);
        iv2.setLayoutParams(layoutParams);
        iv2.setScaleType(ImageView.ScaleType.FIT_XY);
        iv2.setImageResource(R.drawable.two);
        iv3.setLayoutParams(layoutParams);
        iv3.setScaleType(ImageView.ScaleType.FIT_XY);
        iv3.setImageResource(R.drawable.three);
        list.add(iv);
        list.add(iv2);
        list.add(iv3);
        guidePage_ViewPager.setAdapter(new GuidePageAdapter(list));
        addPoint();
    }

    private void addPoint(){
        for (int i = 0;i<3;i++){
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            if (i<1){
                layoutParams.setMargins(0,0,0,0);
            }else {
                layoutParams.setMargins(50,0,0,0);
            }
            ImageView iv = new ImageView(GuidePageActivity.this);
            iv.setLayoutParams(layoutParams);
            iv.setImageResource(R.drawable.weixuanzhon);
            guidePage_point.addView(iv);
            guidePage_point.getChildAt(0).setBackgroundResource(R.drawable.xuanzhon);
        }
    }

}